package com.ruoyi.kanban.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.kanban.domain.SequenceNumber;
import com.ruoyi.kanban.service.ISequenceNumberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 流水号记录Controller
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@RestController
@RequestMapping("/kanban/sequence")
public class SequenceNumberController extends BaseController
{
    @Autowired
    private ISequenceNumberService sequenceNumberService;

    /**
     * 查询流水号记录列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:sequence:list')")
    @GetMapping("/list")
    public TableDataInfo list(SequenceNumber sequenceNumber)
    {
        startPage();
        List<SequenceNumber> list = sequenceNumberService.selectSequenceNumberList(sequenceNumber);
        return getDataTable(list);
    }

    /**
     * 导出流水号记录列表
     */
    @PreAuthorize("@ss.hasPermi('kanban:sequence:export')")
    @Log(title = "流水号记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SequenceNumber sequenceNumber)
    {
        List<SequenceNumber> list = sequenceNumberService.selectSequenceNumberList(sequenceNumber);
        ExcelUtil<SequenceNumber> util = new ExcelUtil<SequenceNumber>(SequenceNumber.class);
        util.exportExcel(response, list, "流水号记录数据");
    }

    /**
     * 获取流水号记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('kanban:sequence:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sequenceNumberService.selectSequenceNumberById(id));
    }

    /**
     * 新增流水号记录
     */
    @PreAuthorize("@ss.hasPermi('kanban:sequence:add')")
    @Log(title = "流水号记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SequenceNumber sequenceNumber)
    {
        return toAjax(sequenceNumberService.insertSequenceNumber(sequenceNumber));
    }

    /**
     * 修改流水号记录
     */
    @PreAuthorize("@ss.hasPermi('kanban:sequence:edit')")
    @Log(title = "流水号记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SequenceNumber sequenceNumber)
    {
        return toAjax(sequenceNumberService.updateSequenceNumber(sequenceNumber));
    }

    /**
     * 删除流水号记录
     */
    @PreAuthorize("@ss.hasPermi('kanban:sequence:remove')")
    @Log(title = "流水号记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sequenceNumberService.deleteSequenceNumberByIds(ids));
    }

    /**
     * 生成临时流水号
     * 
     * @param seqKey 流水号key
     * @param length 流水号长度
     * @return 临时流水号
     */
    @GetMapping("/temp")
    public AjaxResult generateTempSequenceNumber(@RequestParam String seqKey, @RequestParam int length)
    {
        try {
            String sequenceNumber = sequenceNumberService.generateTempSequenceNumber(seqKey, length);
            return success(sequenceNumber);
        } catch (Exception e) {
            return error("生成临时流水号失败：" + e.getMessage());
        }
    }

    /**
     * 生成正式流水号
     * 
     * @param seqKey 流水号key
     * @param length 流水号长度
     * @return 正式流水号
     */
    @PostMapping("/formal")
    public AjaxResult generateFormalSequenceNumber(@RequestParam String seqKey, @RequestParam int length)
    {
        try {
            String sequenceNumber = sequenceNumberService.generateFormalSequenceNumber(seqKey, length);
            return success(sequenceNumber);
        } catch (Exception e) {
            return error("生成正式流水号失败：" + e.getMessage());
        }
    }
}